﻿/*************************************************************************************
* 运行库版本：	4.0.30319.18408
* 类  名  称：	LightNetworkInformation
* 命 名 空间：	IOTILMS.Business
* 文  件  名：	LightNetworkInformation
* 创 建 时间：	2014/5/15 0:59:08
* 作      者：	帅红  shuaihong617@qq.com
* 说      明：
* 修 改 时间：
* 修  改  人：
*************************************************************************************/

using System;
using System.Diagnostics.Contracts;
using System.Net;
using System.Timers;
using FutureTech.ComponentModel;

namespace FutureTech.Network
{
        /// <summary>
        /// 
        /// </summary>
        public class ActivityContext : NotifyPropertyChangedObject
        {
                /// <summary>
                /// Initializes a new instance of the <see cref="ActivityContext" /> class.
                /// </summary>
                /// <param name="id">The unique identifier.</param>
                /// <param name="lostMilliseconds">The lost milliseconds.</param>
                public ActivityContext(string id, int lostMilliseconds)
                {
                        if (BeatTimer == null)
                        {
                                BeatTimer = new Timer(1000);
                                BeatTimer.Start();
                        }

                        Id = id;
                        LostMilliseconds = lostMilliseconds;

                        IsActive = false;
                        PassedMilliseconds = lostMilliseconds;

                        BeatTimer.Elapsed += BeatTimer_Elapsed;
                }

                /// <summary>
                /// 全局心跳计时器
                /// </summary>
                public static Timer BeatTimer { get; private set; }


                /// <summary>
                /// 节点标识
                /// </summary>
                public string Id { get; private set; }

                /// <summary>
                /// 最近一次心跳时间
                /// </summary>
                private DateTime _lastBeatTime = DateTime.Now;

                /// <summary>
                /// 最近一次心跳时间
                /// </summary>
                public DateTime LastBeatTime
                {
                        get { return _lastBeatTime; }
                        private set
                        {
                                _lastBeatTime = value;
                                this.NotifyPropertyChanged(i => i.LastBeatTime);
                        }
                }

                /// <summary>
                /// 是否在线
                /// </summary>
                private bool _isActive;

                /// <summary>
                /// 是否在线
                /// </summary>
                public bool IsActive
                {
                        get { return _isActive; }
                        private set
                        {
                                if (_isActive && !value)
                                {
                                        LostTimes++;
                                }

                                _isActive = value;
                                this.NotifyPropertyChanged(i => i.IsActive);

                                
                        }
                }

                /// <summary>
                /// 掉线次数
                /// </summary>
                private int _lostTimes;

                /// <summary>
                /// 掉线次数
                /// </summary>
                public int LostTimes
                {
                        get { return _lostTimes; }
                        private set
                        {
                                _lostTimes = value;
                                this.NotifyPropertyChanged(i => i.LostTimes);
                        }
                }

                
                /// <summary>
                /// 离线所需时间，单位毫秒
                /// </summary>
                public int LostMilliseconds { get; private set; }

                /// <summary>
                /// 上次心跳已过时间，单位毫秒
                /// </summary>
                private int _passedMilliseconds;

                /// <summary>
                /// 上次心跳已过时间，单位毫秒
                /// </summary>
                public int PassedMilliseconds
                {
                        get { return _passedMilliseconds; }
                        private set
                        {
                                _passedMilliseconds = value;
                                IsActive = _passedMilliseconds < LostMilliseconds;

                                this.NotifyPropertyChanged(i => i.PassedMilliseconds);
                        }
                }

                private void BeatTimer_Elapsed(object sender, ElapsedEventArgs e)
                {
                        PassedMilliseconds += (int)BeatTimer.Interval;
                }

                /// <summary>
                /// 心跳
                /// </summary>
                public void ReceiveHeartBeat()
                {
                        LastBeatTime = DateTime.Now;
                        PassedMilliseconds = 0;
                }
        }
}
